package com.jic.interaction.weUM.service.impl;

import cn.webank.um.login.LoginResult;
import cn.webank.um.login.UMLogin;
import com.alibaba.fastjson.JSON;
import com.jic.interaction.weUM.common.utils.RestResult;
import com.jic.interaction.weUM.service.UserAuthenticationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @ClassName UserAuthenticationServiceImpl
 * TODO
 * @Description
 * @Author chenhongfeng
 * @Date 2020/11/11 14:04
 * @Version 1.0
 **/
@Service
@Slf4j
public class UserAuthenticationServiceImpl implements UserAuthenticationService {
    @Value(value = "${UM.UM_SERVICE_IP}")
    private String UM_SERVICE_IP;
    @Value(value = "${UM.UM_SERVICE_PORT}")
    private String UM_SERVICE_PORT;
    @Value(value = "${UM.UM_SERVICE_APPID}")
    private String UM_SERVICE_APPID;
    @Value(value = "${UM.UM_SERVICE_APPTOKEN}")
    private String UM_SERVICE_APPTOKEN;

    @Override
    public RestResult login(String userId, String userPwd) {
        String userId1 = "zyondchen";
        String userPwd1 = "Abcd123";  //这里是静态密码
        log.info("登录认证，请求参数{}{}", JSON.toJSONString(userId), JSON.toJSONString(userPwd));
        //根据配置参数创建并初始化UNLogin对象，指定超时时间为2秒

        log.info("=====【初始化UNLogin对象】入参======【UM_SERVICE_IP】 = " +UM_SERVICE_IP+"【UM_SERVICE_PORT】 = "+ UM_SERVICE_PORT+"【timeoutSec】 = "+2);
        UMLogin entry = new UMLogin(UM_SERVICE_IP, UM_SERVICE_PORT, 2);
        log.info("=====【初始化UNLogin对象】出参=====【UM_SERVICE_IP】 = ",JSON.toJSONString(entry));


        //子系统鉴权
        log.info("=====【子系统鉴权】入参======【UM_SERVICE_APPID】 = " +UM_SERVICE_APPID+"【UM_SERVICE_APPTOKEN】 = "+ UM_SERVICE_APPTOKEN);
        boolean success = entry.auth(UM_SERVICE_APPID, UM_SERVICE_APPTOKEN);
        log.info("=====【子系统鉴权】出参===== 【success】 = ",JSON.toJSONString(success));


        if (success) {
            //鉴权通过，保存该enry对象
            LoginResult result = entry.login(userId, userPwd);
            log.info("result的值为:{}", result);
            if (0 == result.getRetCode()) {
                log.info("成功");
                //success
                return RestResult.success("0", result);
            } else if (-1 == result.getRetCode()) {
                //验密超时会自动重试1次
                result = entry.loginRetry(userId, userPwd);
                log.info("请求超时或无法连接到服务器");
                return RestResult.error("-1", "请求超时或无法连接到服务器");
            } else if (-2 == result.getRetCode()) {
                log.info("请求参数错误 Illegal Argument");
                return RestResult.error("-2", "请求参数错误 Illegal Argument");
            } else if (1 == result.getRetCode()) {
                log.info("IP、MAC校验失败");
                return RestResult.error("1", "IP、MAC校验失败");
            } else if (2 == result.getRetCode()) {
                log.info("账号未被激活");
                return RestResult.error("2", "账号未被激活");
            } else if (3 == result.getRetCode()) {
                log.info("其他禁用");
                return RestResult.error("3", "其他禁用");
            } else if (4 == result.getRetCode()) {
                log.info("用户名或密码错误");
                return RestResult.error("4", "用户名或密码错误");
            } else if (5 == result.getRetCode()) {
                log.info("需要修改密码-已过密码有效期");
                return RestResult.error("5", "需要修改密码-已过密码有效期");
            } else if (6 == result.getRetCode()) {
                log.info("运行异常");
                return RestResult.error("6", "运行异常");
            } else if (7 == result.getRetCode()) {
                log.info("初始密码未修改");
                return RestResult.error("7", "初始密码未修改");
            } else if (8 == result.getRetCode()) {
                log.info("离职禁用");
                return RestResult.error("8", "离职禁用");
            } else if (9 == result.getRetCode()) {
                log.info("休假禁用");
                return RestResult.error("9", "休假禁用");
            } else if (10 == result.getRetCode()) {
                log.info("账号锁定");
                return RestResult.error("10", "账号锁定");
            } else if (11 == result.getRetCode()) {
                log.info("TOKEN服务异常");
                return RestResult.error("11", "TOKEN服务异常");
            } else if (12 == result.getRetCode()) {
                log.info("UM静态密码登录禁用");
                return RestResult.error("12", "UM静态密码登录禁用");
            } else if (13 == result.getRetCode()) {
                log.info("鉴权错误");
                return RestResult.error("13", "鉴权错误");
            } else if (14 == result.getRetCode()) {
                log.info("时钟错误");
                return RestResult.error("1111", "时钟错误");
            } else {
                //failed
                System.err.println(result.getRetDesc());
            }
        } else {
            log.info("鉴权失败");
            return RestResult.error("1111", "鉴权失败");
        }

        return null;
    }
}
